#!/bin/sh
#                B R L C A D - C O N F I G . I N
# BRL-CAD
#
# Copyright (c) 2007-2025 United States Government as represented by
# the U.S. Army Research Laboratory.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
#
# 3. The name of the author may not be used to endorse or promote
# products derived from this software without specific prior written
# permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
###
#
# BRL-CAD configuration script for determining compilation
# characteristics of a given install.
###

prefix=@prefix@
exec_prefix=@exec_prefix@
includedir=@includedir@
libdir=@libdir@


usage ( ) {
    cat <<EOF
Usage: brlcad-config [OPTIONS] [LIBRARIES]

Options:
    [ --help ]
    [ --version ]
    [ --cflags ]
    [ --ldflags ]
    [ --libs ]
    [ --prefix[=DIR] ]
    [ --libdir[=DIR] ]
    [ --includedir[=DIR] ]

Libraries:
    analyze
    bn
    brlcad (default)
    bu
    dm
    fb
    fft
    gcv
    ged
    optical
    pkg
    rt
    wdb
EOF
    exit 1
}

# need at least a library or an action
if test $# -eq 0; then
    usage 1>&2
fi

libs=""
specified=no
while test $# -gt 0; do
    case "$1" in
	-*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
	*) optarg= ;;
    esac

    case $1 in
	--prefix)
	    echo_prefix=yes
	    specified=yes
	    ;;
	--prefix=*)
	    prefix="$optarg"
	    exec_prefix="$prefix"
	    includedir="${prefix}/include"
	    libdir="${prefix}/lib"
	    ;;
	--includedir)
	    echo_includedir=yes
	    specified=yes
	    ;;
	--includedir=*)
	    includedir="$optarg"
	    ;;
	--libdir)
	    echo_libdir=yes
	    specified=yes
	    ;;
	--libdir=*)
	    libdir="$optarg"
	    ;;
	--version)
	    echo_version=yes
	    specified=yes
	    ;;
	--cflags)
	    echo_cflags=yes
	    specified=yes
	    ;;
	--ldflags)
	    echo_ldflags=yes
	    specified=yes
	    ;;
	--libs)
	    echo_libs=yes
	    specified=yes
	    ;;
	--help)
	    usage 1>&2
	    ;;
	analyze)
	    libs="$libs -lanalyze"
	    ;;
	bn)
	    libs="$libs -lbn"
	    ;;
	brlcad)
	    libs="$libs -lbrlcad"
	    ;;
	bu)
	    libs="$libs -lbu"
	    ;;
	dm)
	    libs="$libs -ldm"
	    ;;
	fb)
	    libs="$libs -lfb"
	    ;;
	fft)
	    libs="$libs -lfft"
	    ;;
	gcv)
	    libs="$libs -lgcv"
	    ;;
	ged)
	    libs="$libs -lged"
	    ;;
	optical)
	    libs="$libs -loptical"
	    ;;
	pkg)
	    libs="$libs -lpkg"
	    ;;
	rt)
	    libs="$libs -lrt"
	    ;;
	wdb)
	    libs="$libs -lwdb"
	    ;;
	*)
	    usage 1>&2
	    ;;
    esac
    shift
done

# determine the behavior of echo
case `echo "testing\c"; echo 1,2,3`,`echo -n testing; echo 1,2,3` in
    *c*,-n*) ECHO_N= ECHO_C='
' ECHO_T='	' ;;
    *c*,*  ) ECHO_N=-n ECHO_C= ECHO_T= ;;
    *)       ECHO_N= ECHO_C='\c' ECHO_T= ;;
esac

# default action is libs and ldflags
if test "x$specified" = "xno" ; then
    echo_libs=yes
    echo_ldflags=yes
fi

if test "x$libs" = "x" ; then
    # default lib is libbrlcad
    libs="-lbrlcad"
fi

if test "x$echo_version" = "xyes" ; then
    echo "@BRLCAD_VERSION@"
fi

if test "x$echo_prefix" = "xyes"; then
    echo "${prefix}"
fi

if test "x$echo_libdir" = "xyes"; then
    echo "${libdir}"
fi

if test "x$echo_includedir" = "xyes"; then
    echo "${includedir}"
fi


check ( ) {
    __cadlibs="$*"
    __result=""
    for new in $__cadlibs ; do
	case "x$new" in
	    x/*)
		new="`basename \"$new\"`"
		new="-l`echo $new | sed 's/^lib//' | sed 's/\.la$//'`"
		;;
	    x-I/include)
		new=""
		;;
	    *)
		;;
	esac

	__found=no
	for already in $__result ; do
	    if test "x$new" = "x$already" ; then
		__found=yes
		break
	    fi
	done
	if test "x$__found" = "xno" ; then
	    __result="$__result $new"
	fi
    done
    echo $__result
}


# stash the info
ANALYZE="@ANALYZE@ @ANALYZE_LIBS@"
BN="@BN@ @BN_LIBS@"
BRLCAD="@BRLCAD@ @BRLCAD_LIBS@"
BU="@BU@ @BU_LIBS@"
DM="@DM@ @DM_LIBS@"
FB="@FB@ @FB_LIBS@"
FFT="@FFT@ @FFT_LIBS@"
GCV="@GCV@ @GCV_LIBS@"
GED="@GED@ @GED_LIBS@"
OPTICAL="@OPTICAL@ @OPTICAL_LIBS@"
PKG="@PKG@ @PKG_LIBS@"
RT="@RT@ @RT_LIBS@"
WDB="@WDB@ @WDB_LIBS@"


cadlibs=""
for lib in $libs ; do
    case $lib in
	-lanalyze)
	    if test "x$analyzeadded" = "x" ; then
		cadlibs="$cadlibs $ANALYZE"
		cadlibs="`check \"$cadlibs\"`"
		analyzeadded=yes
	    fi
	    ;;
	-lbn)
	    if test "x$bnadded" = "x" ; then
		cadlibs="$cadlibs $BN"
		cadlibs="`check \"$cadlibs\"`"
		bnadded=yes
	    fi
	    ;;
	-lbrlcad)
	    if test "x$brlcadadded" = "x" ; then
		cadlibs="$cadlibs $BRLCAD"
		cadlibs="`check \"$cadlibs\"`"
		brlcadadded=yes
	    fi
	    ;;
	-lbu)
	    if test "x$buadded" = "x" ; then
		cadlibs="$cadlibs $BU"
		cadlibs="`check \"$cadlibs\"`"
		buadded=yes
	    fi
	    ;;
	-ldm)
	    if test "x$dmadded" = "x" ; then
		cadlibs="$cadlibs $DM"
		cadlibs="`check \"$cadlibs\"`"
		dmadded=yes
	    fi
	    ;;
	-lfb)
	    if test "x$fbadded" = "x" ; then
		cadlibs="$cadlibs $FB"
		cadlibs="`check \"$cadlibs\"`"
		fbadded=yes
	    fi
	    ;;
	-lfft)
	    if test "x$fftadded" = "x" ; then
		cadlibs="$cadlibs $FFT"
		cadlibs="`check \"$cadlibs\"`"
		fftadded=yes
	    fi
	    ;;
	-lgcv)
	    if test "x$gcvadded" = "x" ; then
		cadlibs="$cadlibs $GCV"
		cadlibs="`check \"$cadlibs\"`"
		gcvadded=yes
	    fi
	    ;;
	-lged)
	    if test "x$gedadded" = "x" ; then
		cadlibs="$cadlibs $GED"
		cadlibs="`check \"$cadlibs\"`"
		gedadded=yes
	    fi
	    ;;
	-loptical)
	    if test "x$opticaladded" = "x" ; then
		cadlibs="$cadlibs $OPTICAL"
		cadlibs="`check \"$cadlibs\"`"
		opticaladded=yes
	    fi
	    ;;
	-lpkg)
	    if test "x$pkgadded" = "x" ; then
		cadlibs="$cadlibs $PKG"
		cadlibs="`check \"$cadlibs\"`"
		pkgadded=yes
	    fi
	    ;;
	-lrt)
	    if test "x$rtadded" = "x" ; then
		cadlibs="$cadlibs $RT"
		cadlibs="`check \"$cadlibs\"`"
		rtadded=yes
	    fi
	    ;;
	-lwdb)
	    if test "x$wdbadded" = "x" ; then
		cadlibs="$cadlibs $WDB"
		cadlibs="`check \"$cadlibs\"`"
		wdbadded=yes
	    fi
	    ;;
	*)
	    if test "x$need_newline" = "xyes" ; then
		echo ""
	    fi
	    echo "Internal Error: improperly added library encountered"
	    exit 2
	    ;;
    esac
done


CPPFLAGS="@CPPFLAGS@"
CPPFLAGS="`check $CPPFLAGS`"
CFLAGS="@CFLAGS@"
CFLAGS="`check $CFLAGS`"
LDFLAGS="@LDFLAGS@"
LDFLAGS="`check $LDFLAGS`"
LIBS="@LIBS@"
LIBS="`check $LIBS`"

# keep track of whether we need a trailing newline
need_newline=no

if test "x$echo_cflags" = "xyes"; then
    echo $ECHO_N "-I${includedir}/brlcad -I${includedir} $CPPFLAGS $CFLAGS $ECHO_C"
    need_newline=yes
fi

if test "x$echo_ldflags" = "xyes"; then
    echo $ECHO_N "-L${libdir} $LDFLAGS $ECHO_C"
    need_newline=yes
fi

if test "x$echo_libs" = "xyes"; then
    echo $ECHO_N "-L${libdir} $cadlibs $LIBS $ECHO_C"
    need_newline=yes
fi

if test "x$need_newline" = "xyes" ; then
    echo ""
fi

# Local Variables:
# tab-width: 8
# mode: sh
# indent-tabs-mode: t
# End:
# ex: shiftwidth=4 tabstop=8
